package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.CompanyHonorDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionTextDao;
import com.copm.demonstrationsystem.dto.introductionfeatures.CompanyHonorDto;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.CompanyHonor;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionImage;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionText;
import com.copm.demonstrationsystem.service.introductionfeatures.CompanyHonorService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 20:57 2018/4/19
 */
@Service
public class CompanyHonorServiceImpl implements CompanyHonorService {

    private static Logger logger = LoggerFactory.getLogger(CompanyHonorServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    IntroductionImageDao introductionImageDao;

    @Autowired
    IntroductionTextDao introductionTextDao;

    @Autowired
    CompanyHonorDao companyHonorDao;

    @Autowired
    ImageDao imageDao;

    @Autowired
    MultiImageAndTextHandle multiImageAndTextHandle;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    /**
     * 获取“品牌荣誉”页面信息
     *
     * @return
     */
    @Override
    public ResponseStatus getCompanyHonor() {
        ResponseStatus status = new ResponseStatus();
        //data传输数据为两个对象：图片 && 文字
        //Object[] obj = new Object[2];
        CompanyHonorDto dto = new CompanyHonorDto();
        //查询图片组信息
        List<IntroductionImage> images = introductionImageDao.findByImageKind(1L);
        for (int i = 0; i < images.size(); i++) {
            IntroductionImage introductionImage = images.get(i);
            Image image = introductionImage.getImage();
            if (null == introductionImage) {
                continue;
            }
            if (null == image) {
                continue;
            }
            transformImagePath.transformImagePath(image);
        }
        //查询文字组信息
        List<IntroductionText> texts = introductionTextDao.findByTextKind(1L);
        //查询背景图片
        CompanyHonor companyHonor = companyHonorDao.findPage();
        if (null == companyHonor) {
            status.setData("查无“品牌荣誉”页面");
            return status;
        }
        Image backgroundImage = companyHonor.getBackgroundImage();
        if (null == backgroundImage) {
            companyHonor.setBackgroundImage(null);
        } else {
            backgroundImage = transformImagePath.transformImagePath(backgroundImage);
        }
        //对象数组保存图片文字对象
//        obj[0] = images;
//        obj[1] = texts;

        dto.setImages(images);
        dto.setTexts(texts);
        dto.setBackgroundImage(backgroundImage);
        status.setStatue(1);
        status.setData(dto);
        logger.info(" 品牌荣誉 页面查询成功！");
        return status;
    }


    /**
     * 添加“品牌荣誉”页面文本
     *
     * @param content
     * @return
     */
    @Override
    public synchronized ResponseStatus addCompanyHonorText(String content) {

        ResponseStatus status = multiImageAndTextHandle.addIntroductionText(content, 1L, 0L);
        logger.info(" 品牌荣誉 页面添加文本成功！");
        return status;

    }

    /**
     * 添加 “品牌荣誉”页面图片
     *
     * @return
     */
    @Override
    public synchronized ResponseStatus addCompanyHonorImage(MultipartFile file) {
        ResponseStatus status = multiImageAndTextHandle.addIntroductionImage(file, 1L);
        logger.info(" 品牌荣誉 页面图片添加成功！");
        return status;
    }

    @Override
    public ResponseStatus addCompanyHonorBackgroundImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (0 == status.getStatue()) {
            logger.info(" 品牌荣誉 页面背景图片添加失败！");
            return status;
        }
        CompanyHonor companyHonor = companyHonorDao.findPage();
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);

        companyHonor.setBackgroundImage(image);
        //保存已上传项目图片的页面对象
        companyHonorDao.save(companyHonor);
        status.setStatue(1);
        logger.info(" 品牌荣誉 页面背景图片添加成功！");
        return status;
    }

    /**
     * 删除“品牌荣誉”页面文本
     *
     * @param id
     * @return
     */
    @Override
    public ResponseStatus deleteCompanyHonorText(Long id) {
        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionText(id, 1L);
        logger.info(" 品牌荣誉 页面文本删除成功！");
        return status;
    }

    /**
     * 删除“品牌荣誉”页面页面图片
     *
     * @param id
     * @return
     */
    @Override
    public ResponseStatus deleteCompanyHonorImage(Long id) {
        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionImage(id);
        logger.info(" 品牌荣誉 页面背景图片删除成功！");
        return status;
    }

    /**
     * 删除“品牌荣誉”页面背景图片
     *
     * @return
     */
    @Override
    public ResponseStatus deleteCompanyHonorBackgroundImage() {
        ResponseStatus status;
        CompanyHonor companyHonor = companyHonorDao.findPage();
        //获取图片
        Image image = companyHonor.getBackgroundImage();
        //删除图片
        status = singlonImageHandle.deleteImage(image);
        //临时获取图片ID
        Long tempId = companyHonor.getBackgroundImage().getId();
        //页面的而背景图片只有一张，删除即置为null
        companyHonor.setBackgroundImage(null);
        //保存已删除的页面对象
        companyHonorDao.save(companyHonor);
        //从图片对象中删除图片
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info(" 品牌荣誉 页面背景图片删除成功！");
        return status;
    }


    /**
     * 修改“企业荣誉”页面文字
     *
     * @param id
     * @return
     */
    @Override
    public ResponseStatus modifyCompanyHonorText(Long id, String content) {
        ResponseStatus status = deleteCompanyHonorText(id);
        if (status.getStatue() == 0) {
            return status;
        }
        status = addCompanyHonorText(content);
        if (0 == status.getStatue()) {
            logger.info(" 品牌荣誉 页面文本修改失败！");
            return status;
        }
        logger.info(" 品牌荣誉 页面文本修改成功！");
        return status;
    }

    /**
     * 修改背景图片
     *
     * @param file
     * @return
     */
    @Override
    public ResponseStatus modifyCompanyHonorBackgroundImage(MultipartFile file) {
        ResponseStatus status;
        CompanyHonor companyHonor = companyHonorDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        if (null == companyHonor) {
            companyHonor = new CompanyHonor();
            //保存对象
            companyHonorDao.save(companyHonor);
        }
        if (null != companyHonor.getBackgroundImage()) {
            status = deleteCompanyHonorBackgroundImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addCompanyHonorBackgroundImage(file);
        if (0 == status.getStatue()) {
            logger.info(" 品牌荣誉 页面背景图片修改失败！");
            return status;
        }
        logger.info(" 品牌荣誉 页面背景图片修改成功！");
        return status;
    }

//    /**
//     * 修改“企业荣誉”页面图片
//     *
//     * @param id
//     * @return
//     */
//    @Override
//    public ResponseStatus modifyCompanyHonorImage(Long id, MultipartFile file) {
//        //删除待修改图片
//        ResponseStatus status = deleteCompanyHonorImage(id);
//        if (status.getStatue() == 0) {
//            return status;
//        }
//        //上传修改图片
//        status = addCompanyHonorImage(file);
//        return status;
//    }
}
